﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using Microsoft.DirectX.DirectDraw;

namespace gametest
{
    public enum BitmapType { SOLID = 0, TRANSPARENT = 1 }

    public class DxImageObject
    {
        /* fields */
        protected double dXPos = 0.0f;
        protected double dYPos = 0.0f;
        protected double dLastXPos = 0.0f;
        protected double dLastYPos = 0.0f;
        protected int iWidth = 0;
        protected int iHeight = 0;
        protected Rectangle objectSizeRect = Rectangle.Empty;
        protected Rectangle rectPosition = Rectangle.Empty;
        protected bool bVisible = true;
        protected Bitmap sourceBitmap;
        protected BitmapType objectType;
        protected Device targetDevice;
        protected SurfaceDescription surfaceDesc;
        protected Surface bitmapSurface;
        protected ColorKey tempKey;
        protected int colKey;

        /* properties */
        public double XPosition
        {
            get { return dXPos; }
            set
            {
                dLastXPos = dXPos; // save last position
                dXPos = value; // save value
                rectPosition = new Rectangle(new Point(Convert.ToInt32(dXPos), rectPosition.Y), new Size(this.Width, this.Height)); // set integer position
            }
        }
        public double YPosition
        {
            get { return dYPos; }
            set
            {
                dLastYPos = dYPos;
                dYPos = value;
                rectPosition = new Rectangle(new Point(rectPosition.X, Convert.ToInt32(dYPos)), new Size(this.Width, this.Height));
            }
        }
        public int Width
        {
            get { return iWidth; }
        }
        public int Height
        {
            get { return iHeight; }
        }
        public bool Visible
        {
            get { return bVisible; }
            set { bVisible = value; }
        }
        public double LastXPosition
        {
            get { return dLastXPos; }
        }
        public double LastYPosition
        {
            get { return dLastYPos; }
        }
        public Rectangle Position
        {
            get { return rectPosition; }
        }
        public Bitmap BitmapOfObject
        {
            get { return sourceBitmap; }
        }
        public Surface SurfaceOfBitmap
        {
            get { return bitmapSurface; }
        }

        /* constructor methods */
        public DxImageObject(Bitmap SourceBitmap, BitmapType ObjectType, int cKey, Device TargetDevice)
        {
            sourceBitmap = SourceBitmap;
            objectType = ObjectType;
            targetDevice = TargetDevice;
            objectSizeRect = new Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height);
            rectPosition = new Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height);
            iWidth = SourceBitmap.Width;
            iHeight = SourceBitmap.Height;
            InitializeSurfaceDescription();
            tempKey = new ColorKey();
            colKey = cKey;
            InitializeSurface(cKey);
        }
        public DxImageObject(BitmapType ObjectType, Device TargetDevice, int cKey, string AssemblyImageResource)
        {
            sourceBitmap = new Bitmap(GetType().Module.Assembly.GetManifestResourceStream(AssemblyImageResource));
            objectType = ObjectType;
            targetDevice = TargetDevice;
            objectSizeRect = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
            rectPosition = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
            iWidth = sourceBitmap.Width;
            iHeight = sourceBitmap.Height;
            InitializeSurfaceDescription();
            tempKey = new ColorKey();
            colKey = cKey;
            InitializeSurface(cKey);
        }
        public DxImageObject(string Resource, BitmapType ObjectType, int cKey, Device TargetDevice)
        {
            sourceBitmap = new Bitmap(Resource);
            objectType = ObjectType;
            targetDevice = TargetDevice;
            objectSizeRect = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
            rectPosition = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
            iWidth = sourceBitmap.Width;
            iHeight = sourceBitmap.Height;
            InitializeSurfaceDescription();
            tempKey = new ColorKey();
            colKey = cKey;
            InitializeSurface(cKey);
        }

        /* support methods */
        private void InitializeSurfaceDescription()
        {
            surfaceDesc = new SurfaceDescription();
            surfaceDesc.SurfaceCaps.OffScreenPlain = true;
            surfaceDesc.Width = objectSizeRect.Width;
            surfaceDesc.Height = objectSizeRect.Height;
        }
        private void InitializeSurface(int colorkey)
        {
            this.bitmapSurface = new Surface(sourceBitmap, surfaceDesc, targetDevice);
            if (this.objectType == BitmapType.TRANSPARENT)
            {
                tempKey.ColorSpaceLowValue = colorkey;
                tempKey.ColorSpaceHighValue = colorkey;
                bitmapSurface.SetColorKey(ColorKeyFlags.SourceDraw, tempKey);
            }
        }

        /* business methods */
        public void DrawBitmap(Surface TargetSurface, float TargetXPos, float TargetYPos)
        {
            this.XPosition = TargetXPos;
            this.YPosition = TargetYPos;
            DrawBitmap(TargetSurface);
        }
        public void DrawBitmap(Surface TargetSurface)
        {
            if (!this.bVisible) return;
            if (this.objectType == BitmapType.TRANSPARENT)
                TargetSurface.DrawFast(rectPosition.X, rectPosition.Y, bitmapSurface, objectSizeRect, DrawFastFlags.SourceColorKey | DrawFastFlags.Wait);
            else if (this.objectType == BitmapType.SOLID)
                TargetSurface.DrawFast(rectPosition.X, rectPosition.Y, bitmapSurface, objectSizeRect, DrawFastFlags.NoColorKey | DrawFastFlags.Wait);
        }
        public void RestoreSurface()
        {
            this.InitializeSurfaceDescription();
            this.InitializeSurface(colKey);
        }
    }
}
